package com.wpw.java8demo;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Description
 * @ClassName FileTest
 * @Author wupengwei
 * @Date 2022/3/23 5:51 PM
 */
public class FileTest {

    @Test
    public void pathAndFileTest() throws IOException {
        Path path = Paths.get("/", "/Users/pengwei/Desktop", "file.txt");
        if (!Files.exists(path)) {
            Files.createFile(path);
        }
        List<String> lines = Arrays.asList("1", "2", "3", "4");
        Files.write(path, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
        List<String> strings = Files.readAllLines(path);
        System.out.println(strings);

    }

    @Test
    public void pathTest() throws IOException {
        Path path = Paths.get("/Users", "/pengwei/Desktop", "file.txt");
        System.out.println(Files.exists(path));
        Files.createFile(path);
        System.out.println(path.getRoot());
    }

    @Test
    public void linesTest() throws IOException {
        Path path = Paths.get("/Users", "/pengwei/Desktop", "file.txt");
        Stream<String> lines = Files.lines(path);
        long count = lines.filter(l -> l.equals("1")).count();
        System.out.println(count);
    }

    @Test
    public void listTest() throws IOException {
        Path path = Paths.get("/Users", "/pengwei/Desktop");
        Stream<Path> list = Files.list(path);
        list.forEach(p -> {
            System.out.println(p);
        });

        System.out.println("==============");
        Stream<Path> walk = Files.walk(path);
        walk.forEach(f -> {
            System.out.println(f);
        });
    }

    @Test
    public void createTempTest() throws IOException {
        Path path = Paths.get("/Users", "/pengwei/Desktop");
        Path tempFile = Files.createTempFile(path, "a", "txt");
        System.out.println(tempFile);
        Path tempDirectory = Files.createTempDirectory(path, "b");
        System.out.println(tempDirectory);

    }

    @Test
    public void copyTest() {
        
    }

}
